<template>
  <div class="main-container">
    <div class="login-container">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
          <h3 class="title">校园新鲜事后台管理系统</h3>
        <el-form-item prop="username" >
          <el-input
            v-model="loginForm.username"
            type="text"
            placeholder="账号"
            prefix-icon="el-icon-user-solid"
            @keyup.enter.native="handleLogin"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="el-icon-s-help"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
          </el-input>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin-bottom: 25px"
        >记住密码
        </el-checkbox
        >
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            style="width: 100%;background-color: #5839ac;color: #ffffff"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="spaceMan-parent">
      <!--动画-->
      <lottie-player
        src="https://assets6.lottiefiles.com/packages/lf20_Ns4TLz.json"
        background="transparent"
        speed="1"
        class="spaceMan"
        loop
        autoplay
      >
      </lottie-player>
    </div>
    <lottie-player
      src="https://assets4.lottiefiles.com/packages/lf20_zprb9vzj.json"
      background="transparent"
      speed="1"
      class="bg"
      loop
      autoplay
    >
    </lottie-player>
    <lottie-player
      src="https://assets4.lottiefiles.com/packages/lf20_zprb9vzj.json"
      background="transparent"
      speed="1"
      class="bg"
      loop
      autoplay
    >
    </lottie-player>
  </div>
</template>

<script>

export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        rememberMe: false
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin () {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post(`admin/login?adminName=${this.loginForm.username}&password=${this.loginForm.password}`) /* 加await返回的是数据 不加返回promise 而返回的数据里面只有data才是api返回的数据 其他的都是axios的 解构并重命名即data重命名为res */
        if (res.status !== 200) return this.$message.error('登录失败！账号或密码错误') /* 参见element 弹框种类 */
        this.$message.success('登录成功')

        // 登录成功后:
        // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中 之后的网络请求中要拿这个token放入请求头中
        //   1.1 项目中除了登录之外的其他API接口，必须在登录之后才能访问 token就是登录令牌
        //   1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中（不是localstorage）
        window.sessionStorage.setItem('token', res.data.token)
        window.sessionStorage.setItem('username', this.loginForm.username)
        // 2. 通过编程式导航跳转到后台主页，路由地址是 /home 但是如果没有拿到token而直接输入地址也不应该跳转 此时要用导航守卫 如果要跳转的是登录页面则直接放行 如果跳转的是其他页面则要判断是否携带token 导航守卫写在router.js里面
        await this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped>
.main-container {
  position: relative;
  width: 100%;
  height: 100%;
}

.login-container {
  z-index: 1;
  position: absolute;
  left: 0;
  height: 100%;
  width: 30%;
  background-color: #ffffff;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 6px;
  box-shadow: 2px 3px 3px #888888;
  opacity: 0.9;
}

.login-form {
  z-index: 1;
  width: 85%;
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
  font-size: 26px;
}

.spaceMan-parent {
  position: absolute;
  right: 0;
  width: 70%;
  height: 100%;
}

.spaceMan {
  z-index: 1;
  position: absolute;
  width: 500px;
  height: 500px;
  left: 0;
  top: 0;
  bottom: 10%;
  right: 30%;
  margin: auto auto;
  transform: translate(-50% -50%);
}

.bg {
  z-index: -2;
  width: 100%;
  overflow: hidden;
}
</style>
